import {_decorator, Button, Color, Component, Label, Sprite} from 'cc';
import {AudioManager} from "db://assets/scripts/common/AudioManager";
import {ResourceUtils} from "db://assets/scripts/common/ResourceUtils";
import {GameManager} from "db://assets/scripts/game/GameManager";
import {EffectManager} from "db://assets/scripts/common/EffectManager";
import {PlayerDataManager} from "db://assets/scripts/common/PlayerDataManager";
import {Skill} from "db://assets/scripts/data/model/Skill";
import {AudioPaths} from "db://assets/scripts/common/AudioPaths";
import {PlayerSkillManager} from "db://assets/scripts/game/role/player/PlayerSkillManager";

const {ccclass, property} = _decorator;

/**
 * 商品控制器
 */
@ccclass('ShopItemController')
export class ShopItemController extends Component {
    /**
     * 技能图标
     */
    @property(Sprite)
    public skillIconSprite: Sprite;
    /**
     * 技能名称
     */
    @property(Label)
    public nameLabel: Label;
    /**
     * 技能描述
     */
    @property(Label)
    public descriptionLabel: Label;
    /**
     * 价格
     */
    @property(Label)
    public goldLabel: Label;
    /**
     * 按钮
     */
    @property(Button)
    public comButton: Button;

    /**
     * 商品
     */
    private skill: Skill;
    /**
     * 是否有足够的钱购买技能
     */
    private purchasable: boolean = false;
    /**
     * 红色
     */
    private redColor: Color = new Color(255, 0, 0, 255);
    /**
     * 黑色
     */
    private blackColor: Color = new Color(0, 0, 0, 255);
    /**
     * 回调
     */
    private callback: Function;

    /**
     * 初始化商品
     *
     * @param itemInfo 商品
     * @param callback 回调
     */
    public init(itemInfo: Skill, callback: Function) {
        this.skill = itemInfo;
        this.callback = callback;

        this.nameLabel.string = itemInfo.name;
        this.descriptionLabel.string = itemInfo.description;
        this.goldLabel.string = `${itemInfo.price}`;

        // 是否可以购买该商品
        this.purchasable = PlayerDataManager.getInstance().getPlayer().gold >= itemInfo.price;
        // 与按钮的可用性进行关联
        this.comButton.interactable = this.purchasable;

        // 当钱不够时
        if (!this.purchasable) {
            // 红色
            this.goldLabel.color = this.redColor;
        } else {
            // 当钱足够时，黑色
            this.goldLabel.color = this.blackColor;
        }

        ResourceUtils.setSpriteFrame(`texture/skillIcon/${itemInfo.icon}`, this.skillIconSprite,);
    }

    /**
     * 点击事件
     */
    public onClick() {
        // 播放声音
        AudioManager.getInstance().playSound(AudioPaths.sellSound);

        // 可购买时
        if (this.purchasable) {
            // 添加技能
            PlayerSkillManager.getInstance().addPlayerSkill(this.skill.id);
            // 扣除金币
            GameManager.getInstance().gameGoldProxy.addGold(-this.skill.price);

            // 特效
            EffectManager.getInstance().loadAndPlayEffect(true, GameManager.getInstance().gamePlayerProxy.getPlayerNode(), "levelUp/levelUp", 1, null, null, false, true, GameManager.getInstance().gameSpeedProxy.getGameSpeed(), true);

            // 回调
            this.callback && this.callback();
        }
    }
}